package com.xf.sys.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.PageUtils;
import com.xf.sys.entity.SysRole;
import com.xf.sys.mapper.SysRoleMapper;
import com.xf.sys.service.ISysRoleService;

/**
 * xf-sys-service
 * SysRoleService.java
 * author:lb
 * 2017年7月4日
 */
@Service
public class SysRoleService implements ISysRoleService {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public int insertSysRole(SysRole sysRole) {
		logger.info("添加角色开始");
		int result=0;
		RedisAtomicLong roleId = new RedisAtomicLong("sysRoleId", redisTemplate.getConnectionFactory());
		SysRole sRole=sysRoleMapper.selectSysRoleByName(sysRole.getRoleName());
		if(null==sRole){
			sysRole.setRoleId((int)roleId.incrementAndGet());
			sysRole.setRoleCode("R"+sysRole.getRoleId());
			result=sysRoleMapper.insertSysRole(sysRole);
		}
		return result;
	}

	@Override
	public int deleteSysRole(Integer[] roleId) {
		logger.info("删除角色开始");
		return sysRoleMapper.deleteSysRole(roleId);
	}

	@Override
	public int updateSysRole(SysRole sysRole) {
		logger.info("修改角色开始");
		return sysRoleMapper.updateSysRole(sysRole);
	}

	@Override
	public PageData selectSysRoleList(PageForm form, SysRole sysRole) {
		logger.info("查询角色列表开始");
		PageUtils.preparePaging(form);
		List<SysRole> list = null;
		PageData listData = null;
		try {
			list =sysRoleMapper.selectSysRoleList(sysRole);
			listData = PageUtils.wrapPageData(list);
		} catch (Exception e) {
			logger.info("查询系統用戶数据失败!");
			e.printStackTrace();
		}
		return listData;
	}

	@Override
	public SysRole selectSysRole(Integer roleId) {
		logger.info("查询角色开始");
		return sysRoleMapper.selectSysRoleById(roleId);
	}

	@Override
	public List<SysRole> selectSysRoleList() {
		return sysRoleMapper.selectSysRoleList(new SysRole());
	}

}
